/*
 * Developer : Jack Matthews
 * Email: developer@droidprofessor.com
 * 
 * Copyright (C) 2010 http://droidprofessor.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.droidprofessor.android.library.phonelicenses;


import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;


/**
 * Helper Class that Gets and sets certain SharedPreferences
 * 
 * @author Jack Matthews
 *
 */
public class PreferencesHelper {
	
	private static final String TAG = "PreferencesHelper";
	
	//Preference Names
	private static final String VERIFIED_ACCOUNT = "phone_licenses_verified_account";
	private static final String VERIFIED_PAYMENT = "phone_licenses_payment_complete";
	private static final String PASSWORD = "phone_licenses_password";
	private static final String USERNAME = "phone_licenses_username";
	private static final String EMAIL = "phone_licenses_email";
	private static final String NAME = "phone_licenses_name";
	
	private static final String TRANSACTION_NO = "phone_licenses_transactionNo";

	
	

	
	
	
	private PreferencesHelper(){};
	

	/**
	 * Return if the account if verified as working
	 */
	public static boolean isAccountVerified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		return prefs.getBoolean(VERIFIED_ACCOUNT, false);
	}


	/**
	 * Is the payment verified
	 * 
	 * @param cnx
	 * @return
	 */
	 static boolean isPaymentVerified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, VERIFIED_PAYMENT+" "+prefs.getBoolean(VERIFIED_PAYMENT, false));
		return prefs.getBoolean(VERIFIED_PAYMENT, false);		
	}

	
	/**
	 * Return the name of the user
	 */
	 static String getName(final Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, "Name: "+prefs.getString(NAME, ""));
		return prefs.getString(NAME, "");
	}
	

	/**
	 * Return the username
	 */
	 public static String getUsername(final Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, "Username: "+prefs.getString(USERNAME, ""));
		return prefs.getString(USERNAME, "");
	}

	
	/**
	 * Return the password
	 */
	 static String getPassword(final Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, "Password: "+prefs.getString(PASSWORD, ""));
		return prefs.getString(PASSWORD, "");
	}
	
	
	/**
	 * Return the password
	 */
	 static String getEmail(final Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, "Email: "+prefs.getString(EMAIL, ""));
		return prefs.getString(EMAIL, "");
	}
	
	
	/**
	 * Return the transaction no from Paypal API
	 */
	 static String getTransactionNo(final Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		Log.d(TAG, "Transaction No: "+prefs.getString(TRANSACTION_NO, ""));
		return prefs.getString(TRANSACTION_NO, "");
	}

	
	
	/**
	 * Update the username, password and email in shared preferences
	 */
	 public  static void updatePreferences(Context cnx, String name, String username, String password, String email) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putString(NAME, name);
		editor.putString(USERNAME, username);
		editor.putString(PASSWORD, password);
		editor.putString(EMAIL, email);
		editor.commit();
	}


	 static void updatePreferences(Context cnx, String username, String password) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putString(USERNAME, username);
		editor.putString(PASSWORD, password);
		editor.commit();
	}

	 static void setAccountVerified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putBoolean(VERIFIED_ACCOUNT, true); //indicates if the user account has been verified as working
		editor.commit();
	}

	 static void setPaymentVerified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putBoolean(VERIFIED_PAYMENT, true); //indicates if the user account has been verified as working
		editor.commit();	
	}
	
	
	public static void setAccountUnverified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putBoolean(VERIFIED_ACCOUNT, false); //indicates if the user account has been verified as working
		editor.commit();
	}


	 static void setPaymentUnverified(Context cnx) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putBoolean(VERIFIED_PAYMENT, false); //indicates if the user account has been verified as working
		editor.commit();	
	}


	 static void setTransactionNo(Context cnx, String number) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cnx);
		SharedPreferences.Editor editor  = prefs.edit();
		editor.putString(TRANSACTION_NO, number); //indicates if the user account has been verified as working
		editor.commit();
	}

}
